package cn.wolfcode.producer._02_send_type;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.CountDownLatch;

public class Producer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producerDemo");
        defaultMQProducer.setSendMsgTimeout(20000);
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");

            defaultMQProducer.start();
        CountDownLatch countDownLatch = new CountDownLatch(20);


        /*producer.setSendMsgTimeout(10000);*/

        Message message = new Message("02-send-type","你好RocketMq".getBytes());
        for(int i=0;i<20;i++){

                defaultMQProducer.send(message, new SendCallback() {

                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println("消息发送成功");
                        countDownLatch.countDown();
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        System.out.println("消息发送失败");
                        countDownLatch.countDown();
                    }
                });

        }
        countDownLatch.await();
       /* System.out.println(sendResult.getSendStatus());
        System.out.println(sendResult.getMsgId());*/
       Thread.sleep(50000);
        defaultMQProducer.shutdown();
    }



}
